<?php
class DealAction extends BaseAction {
    private $deal_info = array();

    private function init_deal_page($deal_info)
    {
        $data = array();
        $data['page_title'] = $deal_info['name'];

        if($deal_info['seo_title']!=""){
            $data['seo_title'] = $deal_info['seo_title'];
        }
        if($deal_info['seo_keyword']!=""){
            $data['seo_keyword'] = $deal_info['seo_keyword'];
        }
        if($deal_info['seo_description']!=""){
            $data['seo_description'] = $deal_info['seo_description'];
        }
        $deal_info['tags_arr'] = preg_split("/[ ,]/",$deal_info['tags']);


        $deal_info['support_amount_format'] = number_price_format($deal_info['support_amount']);
        $deal_info['limit_price_format'] = number_price_format($deal_info['limit_price']);
        $deal_info['remain_support_percent'] = ($deal_info['limit_price'] <= $deal_info['support_amount']) ? '100' : ($deal_info['support_amount']/$deal_info['limit_price']*100);
        $deal_info['remain_days'] = ($deal_info['end_time'] >= time()) ? floor(($deal_info['end_time']-time())/86400) : 0;
        $deal_info['remain_days_percent'] = ($deal_info['end_time'] >= time()) ? intval((((time()-$deal_info['begin_time'])/86400)/(($deal_info['end_time']-time())/86400))*100) : 0;
        $deal_info['percent'] = round($deal_info['support_amount']/$deal_info['limit_price']*100);
        $deal_info['remain_days_status'] = '';
        $deal_info['remain_support_status'] = '';
        if($deal_info['begin_time'] > time()){
            $deal_info['remain_days_status'] = '暂未上线';
            $deal_info['remain_support_status'] = '暂未开始';
        }
        if($deal_info['end_time'] < time() && $deal_info['end_time'] != 0){
            $deal_info['remain_days_status'] = '已经过期';
            if($deal_info['limit_price'] <= $deal_info['support_amount']){
                $deal_info['remain_support_status'] = '达成目标';
            }else{
                $deal_info['remain_support_status'] = '众筹失败';
            }
        }
        if(($deal_info['end_time'] > time() || $deal_info['end_time'] == 0 ) && $deal_info['begin_time'] < time()){
            $deal_info['remain_days_status'] = '剩余时间';
            if($deal_info['limit_price'] <= $deal_info['support_amount']){
                $deal_info['remain_support_status'] = '达成目标';
            }else{
                $deal_info['remain_support_status'] = '正在众筹';
            }
        }
        $deal_info['description'] = stripslashes($deal_info['description']);
        $deal_info['description'] = str_replace('./','/',$deal_info['description']);
        $data['deal_info'] = $deal_info;

        $deal_item_list = M('deal_item')->where(array('deal_id'=>$deal_info['id']))->order('price asc')->select();
        foreach($deal_item_list as $k=>$v)
        {
            $deal_item_list[$k]['images'] = M('deal_item_image')->where(array('deal_id'=>$deal_info['id'],'deal_item_id'=>$v['id']))->select();
            $deal_item_list[$k]['price_format'] = number_price_format($v['price']);
            $deal_item_list[$k]['support_full'] = ($deal_item_list[$k]['is_limit_user'] == 1 && ($deal_item_list[$k]['limit_user'] <= $deal_item_list[$k]['support_count'])) ? 1 : 0;
            $deal_item_list[$k]['limit_remain'] = intval($deal_item_list[$k]['limit_user']-$deal_item_list[$k]['support_count']);

        }
        $data['deal_item_list'] = $deal_item_list;

        if($deal_info['user_id']>0)
        {
            $deal_user_info = M('user')->where(array('id'=>$deal_info['user_id'],'is_effect'=>'1'))->field('id,user_name,province,city,intro,login_time')->find();
            if($deal_user_info)
            {
                $deal_user_info['weibo_list'] = M('user_weibo')->where(array('user_id'=>$deal_user_info['id']))->find();
                $data['deal_user_info'] = $deal_user_info;
            }
        }

        return $data;
    }

    private function get_deal_info(){
        $id = intval($_REQUEST['id']);
        $condition = array();
        $condition['a.id'] = $id;
        $condition['a.is_delete'] = '0';
        if($this->get_login_status()){
            $condition['_string'] = "(a.is_effect = '1' or (a.is_effect != '1' and a.user_id = '{$this->uid}'))";

        }else{
            $condition['a.is_effect'] = '1';
        }
//        $this->deal_info = M('deal')->alias('a')->join('__USER__ b on a.user_id = b.id')->where($condition)->find();
        $this->deal_info = M('deal')->alias('a')->where($condition)->find();
        $where = array('id'=>$this->deal_info['user_id']);
        $userInfo = M('user')->where($where)->find();
        $this->deal_info['ex_real_name'] = $userInfo['ex_real_name'];
        if(!$this->deal_info)
        {
            app_redirect(url("index"));
        }

        if($this->deal_info['is_effect']==1)
        {
            log_deal_visit($this->deal_info['id']);
        }

        if($this->deal_info['is_effect']==1){
            $deal_faq_list = M('deal_faq')->where(array('deal_id'=>$this->deal_info['id']))->order('sort asc')->select();
        }
        else{
            $deal_faq_list = M('deal_faq')->where(array('deal_id'=>$this->deal_info['id']))->order('sort asc')->select();
        }

        $fav_status = 0;
        if($this->get_login_status()){
            $fav_status = M('deal_fav')->where(array('deal_id'=>$id,'user_id'=>$this->uid))->count();
        }
        $this->assign('fav_status',$fav_status);

        $this->assign("deal_faq_list",$deal_faq_list);
        $this->assign($this->init_deal_page($this->deal_info));
    }

    public function index(){

    }


    /**
     * 项目状态
     */
    public function status(){
        $this->get_deal_info();
        $id = intval($_REQUEST['id']);
        $list = array();
        $data = M('deal_news')->where(array('deal_id'=>$id))->order('create_time desc')->select();
        if(is_array($data)){
            foreach($data as $k=>$v){
                $month = date('Y-m',$v['create_time']);
                $v['day'] = date('d',$v['create_time']);
                $list[$month][] = $v;
            }
        }
        if(!$this->get_login_status()){
            $this->assign('has_order','0');
        }else{
            if($this->deal_info['user_id'] == $this->uid){
                $has_order = 1;
            }else{
//                $has_order = 1;
                $has_order = M('deal_order')->where(array('deal_id'=>$id,'user_id'=>$this->uid))->count();
            }
            $this->assign('has_order',intval($has_order));
        }
        if($this->deal_info['user_id'] == $this->uid){
            $this->assign('project_owner','1');
        }
        $this->assign('list',$list);

        $this->display();
    }

    /**
     * 发布项目动态评论
     */
    public function post_status(){
        $this->require_login();
        if($this->isPost()) {
            $deal_id = $_REQUEST['deal_id'];
            $content = strim($_REQUEST['content']);
            $this->check_empty($content, '项目动态内容不能为空');
            $deal_info = M('deal')->where(array('id' => $deal_id, 'user_id' => $this->uid))->find();
            if ($deal_info) {
                if($deal_info['is_effect'] != '1'){
                    $this->return_status('0', '项目暂未上线，不能发表项目动态');
                }
                $data = array();
                $data['deal_id'] = $deal_id;
                $data['user_id'] = $this->uid;
                $data['comment_total'] = '0';
                $data['create_time'] = time();
                $data['content'] = $content;
                if (M('deal_news')->data($data)->add()) {
                    $this->return_status('1', '项目动态发布成功');
                } else {
                    $this->return_status('0', '项目动态发布失败,请联系系统管理员');
                }
            } else {
                $this->return_status('0', '项目信息不存在或者该项目不属于您');
            }
        }
    }

    /**
     * 项目动态评论
     */
    public function status_comment_ajax(){
        $deal_id = intval($this->_get('deal_id'));
        $id = intval($this->_get('id'));
        $data = array();
        $data['news_id'] = $id;
        $data['deal_id'] = $deal_id;
        $list = M('deal_news_comment')->alias('a')->join('__USER__ b on a.user_id = b.id')->where($data)->field("a.*,b.avatar,FROM_UNIXTIME(a.create_time,'%Y-%m-%d %H:%I:%S') as create_day")->order('create_time asc')->select();
        $json = array();
        $json['total'] = count($list);
        foreach($list as $key=>$value){
            $tmp_avatar = pathinfo($value['avatar']);
            $value['avatar_big'] = sprintf('%s/%s_big.%s',$tmp_avatar['dirname'],$tmp_avatar['filename'],$tmp_avatar['extension']);
            $value['avatar_middle'] = sprintf('%s/%s_middle.%s',$tmp_avatar['dirname'],$tmp_avatar['filename'],$tmp_avatar['extension']);
            $value['avatar_small'] = sprintf('%s/%s_small.%s',$tmp_avatar['dirname'],$tmp_avatar['filename'],$tmp_avatar['extension']);
            $json['data'][] = $value;
        }
        exit(json_encode($json,JSON_HEX_TAG));
    }

    /**
     * 项目动态评论
     */
    public function status_comment(){
        if($this->isPost()){
            $deal_id = intval($this->_post('deal_id'));
            $id = intval($this->_post('id'));
//            $content = strip_tags($this->_post('content'));
            $content = stripslashes(urldecode($this->_post('content')));
            $content = str_replace('./','/',$content);
            if(!$this->get_login_status()){
                exit(json_encode(array('status'=>0,'info'=>'您没有登陆')));
            }
            $deal_info = M('deal')->where(array('id'=>$deal_id))->find();
            if(!isset($deal_info['id'])){
                exit(json_encode(array('status'=>0,'info'=>'项目不存在')));
            }
            if($deal_info['user_id'] == $this->uid){
                $has_order = 1;
            }else{
                $has_order = M('deal_order')->where(array('deal_id'=>$deal_id,'user_id'=>$this->uid))->count();
            }
            if(intval($has_order) == 0){
                //exit(json_encode(array('status'=>0,'info'=>'您不是项目支持者')));
            }
            $data = array();
            $data['deal_id'] = $deal_id;
            $data['news_id'] = $id;
            $data['user_id'] = $this->uid;
            $data['user_name'] = $this->username;
            $data['create_time'] = time();
            $data['content'] = $content;
            $this->check_empty($data['content'],'评论内容不能为空');
            if(M('deal_news_comment')->data($data)->add()){
                M('deal_news')->where(array('id'=>$id))->data(array('comment_total'=>array('exp','comment_total+1')))->save();
                exit(json_encode(array('status'=>1,'info'=>'发表评论成功')));
            }else{
                exit(json_encode(array('status'=>0,'info'=>'发表评论失败')));
            }
        }
    }

    /**
     * 项目自我介绍
     */
    public function intro(){
        $this->get_deal_info();
        $this->display();
    }


    /**
     * 项目QA
     */
    public function qa(){
        $id = intval($_REQUEST['id']);
        $condition = array();
        $condition['deal_id'] = $id;
        $list = M('deal_faq')->where($condition)->order('sort desc')->select();
        $this->assign('list',$list);
        $this->get_deal_info();
        $this->display();
    }

    /**
     * 评论
     */
    public function comment(){
        $this->get_deal_info();
        $id = intval($this->_get('id'));
        $project_master = '0';
        $page_size = 10;
        if($this->deal_info['user_id'] == $this->uid){
            $has_order = 1;
            $project_master = '1';
            $total = M('deal_comment')->alias('a')->where(array('a.deal_id'=>$id,'a.pid'=>'0'))->count();
            $Page = new Page($total,$page_size);
            $this->assign('page',$Page->show_front());
            $this->assign("current_page",$Page->nowPage);
            $list = M('deal_comment')->alias('a')->join('__USER__ b on a.user_id = b.id')->where(array('a.deal_id'=>$id,'a.pid'=>'0'))->field('a.*,b.avatar')->order('create_time desc')->limit($Page->firstRow,$Page->listRows)->select();
        }else{
            $total = M('deal_comment')->alias('a')->where(array('a.deal_id'=>$id,'a.pid'=>'0','a.reply_status'=>'1'))->count();
            $Page = new Page($total,$page_size);
            $this->assign('page',$Page->show_front());
            $this->assign("current_page",$Page->nowPage);
            $list = M('deal_comment')->alias('a')->join('__USER__ b on a.user_id = b.id')->where(array('a.deal_id'=>$id,'a.pid'=>'0','a.reply_status'=>'1'))->field('a.*,b.avatar')->order('create_time desc')->limit($Page->firstRow,$Page->listRows)->select();
            $has_order = M('deal_order')->where(array('deal_id'=>$id,'user_id'=>$this->uid))->count();
        }
        foreach($list as $k=>$v){
            if(empty($v['avatar'])){
                $list[$k]['avatar_big'] = '/avatar/noavatar_big.png';
                $list[$k]['avatar_middle'] = '/avatar/avatar_middle.png';
                $list[$k]['avatar_small'] = '/avatar/avatar_small.png';
            }else{
                $tmp_avatar = pathinfo($v['avatar']);
                $list[$k]['avatar_big'] = sprintf('%s/%s_big.%s',$tmp_avatar['dirname'],$tmp_avatar['filename'],$tmp_avatar['extension']);
                $list[$k]['avatar_middle'] = sprintf('%s/%s_middle.%s',$tmp_avatar['dirname'],$tmp_avatar['filename'],$tmp_avatar['extension']);
                $list[$k]['avatar_small'] = sprintf('%s/%s_small.%s',$tmp_avatar['dirname'],$tmp_avatar['filename'],$tmp_avatar['extension']);
            }
        }
        $this->assign('has_order',intval($has_order));
        if(!$this->get_login_status()){
            $this->assign('has_order','0');
        }

        $this->assign('list',$list);
        $this->assign('project_master',$project_master);
        $this->display();
    }

    /**
     * 提交评论
     */
    public function comment_do(){
        if($this->isPost()) {
            if(!$this->get_login_status()){
                $this->return_status('0','您没有登录');
            }
            $data = array();
            $id = intval($this->_post('id'));
            $deal_id = intval($this->_post('deal_id'));
            $content = stripslashes(urldecode($this->_post('content')));
            $content = str_replace('./','/',$content);
            $deal_info = M('deal')->where(array('id'=>$deal_id))->find();
            if(!isset($deal_info['id'])){
                $this->return_status('0','项目不存在');
            }
            $has_order = 1;
//            if($deal_info['user_id'] == $this->uid){
//                $has_order = 1;
//            }else{
//                $has_order = M('deal_order')->where(array('deal_id'=>$id,'user_id'=>$this->uid))->count();
//            }
//            if(intval($has_order) == 0){
//                $this->return_status('0','你不是项目支持者');
//            }
            if ($id != 0) {
                if($deal_info['user_id'] != $this->uid){
                    $this->return_status('0','只有项目发起人能回复');
                }
                $comment_info = M('deal_comment')->where(array('deal_id' => $deal_id, 'id' => $id))->find();
                if(!isset($comment_info['id'])){
                    $this->return_status('0','您要回复的评论不存在');
                }
                $this->check_empty($content,'回复内容不能为空');
                $data['reply_content'] = $content;
                $data['reply_time'] = time();
                $data['reply_status'] = '1';
                $status = M('deal_comment')->where(array('deal_id' => $deal_id, 'id' => $id))->data($data)->save();
                if($status){
                    $this->return_status('1','回复评论成功');
                }else{
                    $this->return_status('0','回复评论失败');
                }
            }
            $this->check_empty($content,'评论内容不能为空');
            $data['deal_id'] = $deal_id;
            $data['content'] = $content;
            $data['user_id'] = $this->uid;
            $data['create_time'] = time();
            $data['user_name'] = $this->username;
            $data['pid'] = $id;
            $data['deal_user_id'] = $deal_info['user_id'];
            $data['deal_user_name'] = $deal_info['user_name'];
            if(M('deal_comment')->data($data)->add()){
                $this->return_status('1','发表评论成功');
            }else{
                $this->return_status('0','发表评论失败，请联系客服');
            }
        }
    }




    public function show(){
        $this->get_deal_info();
        $this->display();
    }
}